/** @file gui_list.cpp
 */
#include "frgui.h"
#include "widget.h"
#include "gui_list.h"
#include "string.h"
#include "bitmap.h"

ListItem::ListItem(char * t_name)
{
    next=0;
    prev=0;
    int i;
    name =t_name;
}

List::List(HWND parent):Widget(parent)
{
    name ="List";
    
    activable =1;
    itemList =0;
    index =0;
    max_index=0;

    keyShift =0;
    font =font_STANDARD;
}
List::~List()
{
    ListItem *t_item;

    while(t_item =getFirstItem(&itemList))
    {
        removeItem(&itemList, t_item);
        delete t_item;
    }
}

void List::setupWidgets()
{
}

void List::draw(Rect *r)
{
    char valueW, valueB;
    pDC->fillRect(rect.x0, rect.y0, rect.x1,rect.y1, 0x0);
    pDC->drawRect(rect.x0, rect.y0, rect.x1, rect.y1, 0x0F);

    //pDC->drawBmp(rect.x0+1, rect.y0+1, rect.x0+1+bitmapUp.w, rect.y0+1+bitmapUp.h, bitmapUp.data);
    if(isActive())
    {
        valueW=0x0F; valueB=0x0;
    }
    else
    {
        valueW=0x0; valueB=0x00;
    }
    
    if(index==0)
    {
        pDC->drawString(rect.x0+1, rect.y0+1, rect.x1, rect.y1, "<", valueB, valueW, font);
    }else if(index ==max_index-1)
    {
        pDC->drawString(rect.x0+1, rect.y0+1, rect.x1, rect.y1, ">", valueB, valueW, font);
    }
    else
    {
        pDC->drawString(rect.x0+1, rect.y0+1, rect.x1, rect.y1, "=", valueB, valueW, font);
    }
    pDC->drawString(rect.x0+1+font->width+1, rect.y0+1, rect.x1, rect.y1, getText(), 0x0F, 0x00, font);
    
    pDC->dirty(max(r->x0,rect.x0), max(r->y0,rect.y0), min(r->x1,rect.x1), min(r->y1,rect.y1));
    Widget::draw(r);
}

int List::eventHandler(HWND hWnd, int iMsg, WPARAM wParam, LPARAM lParam)
{
    switch(iMsg)
    {
    case MSG_KEY_DOWN:
        switch(wParam)
        {
        case KEY_UP:
            if(index>0) index--;
            draw(&rect);
            signal[SIGNAL_LIST_CHANGED].emitSignal();
            break;
        case KEY_DOWN:
            if(index <max_index-1)index++;
            draw(&rect);
            signal[SIGNAL_LIST_CHANGED].emitSignal();
            break;

            //default event process
        default:
            if(keyShift)
            {
                switch(wParam)
                {
                 case KEY_ENTER:
                     sendMessage(this, MSG_KEY_DOWN, KEY_ESC, 0);
                     break;
                 case KEY_PREV:
                     sendMessage(this, MSG_KEY_DOWN, KEY_UP, 0);
                     break;
                 case KEY_NEXT:
                     sendMessage(this, MSG_KEY_DOWN, KEY_DOWN, 0);
                     break;

                 case KEY_ESC:
                     keyShift =0;
                     break;
                }
            }
            else
            {
#if defined(CONFIG_KEY_SHIFT)
                switch(wParam)
                {
                case KEY_ENTER:
                    keyShift=1;
                    break;
                }
#endif
                Widget::eventHandler(hWnd, iMsg, wParam, lParam);
            }
            break;
        }
        break;

    }

    return 0;
}


void List::insertItem(ListItem **list, ListItem *item)
{
    ListItem *t_item;
    
    if(t_item =getFirstItem(list))
    {
        while(t_item->next)
        {
            t_item =t_item->next;
        }
        t_item->next =item;
        item->prev =t_item;
    }
    else
    {
        *list =item;
    }
}

void List::removeItem(ListItem **list, ListItem *item)
{
    if(item->prev)
    {
        item->prev->next =item->next;
        if(item->next)
        {
            item->next->prev =item->prev;
        }
        
        item->prev =NUL;
        item->next =NUL;
    }
    else
    {
        if(item->next)
        {
            item->next->prev =item->prev;
        }
        *list =item->next;
        
        item->prev =NUL;
        item->next =NUL;
    }
}

void List::addItem(char *name)
{
    ListItem *t_item =new ListItem(name);
    t_item->index =max_index;
    insertItem(&itemList, t_item);
    max_index ++;

    rect.x1 =max(rect.x1 ,  rect.x0 + 3 + (font->width+1)*(strlen(name) +1));
    rect.y1 =rect.y0 + font->height +2;
}

int List::getIndex()
{
    return index;
}

void List::setIndex(int x)
{
    if(x<max_index)
    {
        index =x;
    }
}

void List::setFont(FONT *font)
{
    this->font =font;
    //rect.x1 =rect.x0 + font->width +1;
    //rect.y1 =rect.y0 + font->height;
}

/// get text of current index
char *List::getText()
{
    ListItem *t_item;
    for(t_item=getFirstItem(&itemList); t_item; t_item =t_item->next)
    {
        if(t_item->index ==index)
        {
            return t_item->name;
        }
    }

    return 0;
}

